package com.lcz.mytest.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lcz.mytest.R;
import com.lcz.mytest.activity.MainActivity;
import com.lcz.mytest.activity.PresentStockChartActivity;
import com.lcz.mytest.adapter.StockListAdapter;
import com.lcz.mytest.db.StockAPI;
import com.lcz.mytest.domain.StockList;
import com.lcz.mytest.utils.ViewUtils;
import com.lcz.mytest.widget.CustomProgressDialog;

import net.tsz.afinal.http.AjaxCallBack;

import java.util.ArrayList;
import java.util.List;

public class SZAllFragment extends Fragment {
    private FrameLayout frameLayout;
    private View view_SZAll;
    private PullToRefreshListView sz_listview;
    private int page = 1;
    private List<StockList.ResultBean.DataBean> listData = new ArrayList<>();
    private StockListAdapter adapter;
    private CustomProgressDialog loading_dialog;
    private String type;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (frameLayout == null) { // 之前的frameLayout 已经记录了一个爹了 爹是之前的ViewPager
            frameLayout = new FrameLayout(getActivity());
            view_SZAll = inflater.inflate(R.layout.sz_list, null);
            init(); // 在FrameLayout中 添加不同的界面
        } else {
            ViewUtils.removeParent(frameLayout);// 移除frameLayout之前的爹
        }
        return frameLayout; // 拿到当前viewPager 添加这个framelayout
    }

    private void init() {
        if (view_SZAll != null) {
            sz_listview = (PullToRefreshListView) view_SZAll.findViewById(R.id.sz_listview);
            sz_listview.setMode(PullToRefreshBase.Mode.BOTH);
            showList(page);
            sz_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                //下拉刷新
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                    page = 1;
                    showList(page);
                }

                //上拉加载
                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                    String label = DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
                            DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                    refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                    page++;
                    showList(page);
                }
            });
        }
        sz_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                type = ((MainActivity) getActivity()).getType();
                if (type.equals("历史数据")) {
                    Toast.makeText(getActivity(), "暂无历史数据", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), listData.get(position - 1).getName(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(), PresentStockChartActivity.class);
                    intent.putExtra("symbol", listData.get(position - 1).getSymbol());
                    intent.putExtra("name", listData.get(position - 1).getName());
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getActivity().startActivity(intent);
                }
            }
        });
        frameLayout.addView(view_SZAll, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        view_SZAll.setVisibility(View.VISIBLE);
    }

    private void showList(final int page) {

        AjaxCallBack callBack = new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                StockList szList = gson.fromJson(result, StockList.class);
                List<StockList.ResultBean.DataBean> list = szList.getResult().getData();
                if (list != null) {
                    upData(list);
                    sz_listview.onRefreshComplete();//刷新完成
                    loading_dialog.dismiss();
                } else {
                    Toast.makeText(getActivity(), "加载数据失败", Toast.LENGTH_SHORT).show();
                }
                System.out.println(gson.toJson(szList));
            }
        };
        loading_dialog = new CustomProgressDialog(getActivity(), "加载中");
        loading_dialog.show();
        StockAPI.getSZAll(page, callBack);
    }

    //页面更新处理
    private void upData(List<StockList.ResultBean.DataBean> list) {
        if (page == 1) {
            listData.clear();//下拉刷新时清空
        }
        listData.addAll(list);
        if (adapter == null) {
            adapter = new StockListAdapter(listData, getActivity());
            sz_listview.setAdapter(adapter);
        } else {
            adapter.upDataList(listData);
            adapter.notifyDataSetChanged();
        }
    }

}
